package com.ocean.littermanagement.event;

import com.ocean.LitterApplication;
import com.ocean.littermanagement.config.ApplicationConfig;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.ItemEntity;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.event.TickEvent;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
 * 监听
 *
 * @author zhang
 * @since 2024/3/26
 */
@Mod.EventBusSubscriber(modid = LitterApplication.MODID, value = Dist.DEDICATED_SERVER)
public class ModEvents {

    private static int timer = ApplicationConfig.TIMER.get() * 60 * 20;

    private static Iterable<ServerLevel> loadedWorlds;

    private static MinecraftServer serverLevel;

    @SubscribeEvent
    public static void onPlayerStartTracking(PlayerEvent.StartTracking event) {
        if (event.getTarget().getServer() != null) {
            loadedWorlds = event.getTarget().getServer().getAllLevels();
            serverLevel = event.getTarget().getServer();
        }
    }

    @SubscribeEvent
    public static void serverTickEvent(TickEvent.ServerTickEvent event) {
        if (loadedWorlds != null && serverLevel != null) {
            if (event.phase == TickEvent.Phase.START) {
                timer--;
                // 还有5分钟时
                if (timer == 6000) {
                    sendReminder(5);
                } else if (timer == 1200) {
                    sendReminder(1);
                } else if (timer == 0) {
                    // 时间到，清理掉落物
                    cleanupDroppedItems();
                    resetTimer();
                }
            }
        }
    }


    private static void sendReminder(int timer) {
        List<ServerPlayer> players = serverLevel.getPlayerList().getPlayers();
        String message = ApplicationConfig.MESSAGE.get();
        System.out.println(message);
        String format = String.format(message, timer);
        players.forEach(player -> player.sendSystemMessage(Component.literal(format)));
    }

    private static void cleanupDroppedItems() {
        loadedWorlds.forEach(world -> {
            Iterable<Entity> all = world.getEntities().getAll();
            List<ItemEntity> collect = StreamSupport.stream(all.spliterator(), false)
                    .filter(entity -> entity instanceof ItemEntity)
                    .map(entity -> (ItemEntity) entity)
                    .collect(Collectors.toList());
            collect.forEach(res -> res.remove(Entity.RemovalReason.DISCARDED));
        });
    }

    private static void resetTimer() {
        timer = ApplicationConfig.TIMER.get() * 60 * 20;
    }
}
